package map;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//多线程下的map测试
public class Main {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        Main myMain = new Main();
        Map<String, Integer> map = new ConcurrentHashMap<>();
        map.put("alexnine", 10000);
        for (int i = 0; i < 5; i++) {
            executorService.execute(() -> {
                myMain.deleteMap(map);
            });
        }

        Thread.sleep(1);
        System.out.println(map.get("alexnine"));

        Thread.sleep(1000);
        System.out.println(map.get("alexnine"));

        Thread.sleep(1000);
        System.out.println(map.get("alexnine"));
    }

    public void deleteMap(Map<String, Integer> map) {
//        Map map1 = new HashMap();
        int deduceMoney;
//        synchronized (map1){
//
//        }
        for (int i = 0; i < 1000; i++) {

            deduceMoney = map.get("alexnine") - 1;
            if (deduceMoney >= 0) {
                map.put("alexnine", deduceMoney);
            }

        }
    }
}
